package com.medicine.hbp.fragment.x;

import java.lang.ref.WeakReference;
import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.medicine.hbp.adapter.FragmentAdapter01;
import com.medicine.hbp.adapter.ImageFragmentAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.AdlnfoListVo;
import com.medicine.hbp.mob.AdlnfoVo;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.mob.laberInfoVo;
import com.medicine.hbp.sp.InterestSp;
import com.medicine.hbp.sp.UtilsSp;
import com.medicine.hbp.ui.ClassificationDetailActivity;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

public class Fragment01  extends Fragment implements IXListViewListener {
	private View view;
	private XListView xlistview;
	private FragmentAdapter01 adapter;

	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;

	private List<artGroupVo> artGroupVos;
	protected WeakReference<View> mRootView;

	private ImageView NoNetWork;
	@Override
	public android.view.View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (mRootView == null || mRootView.get() == null) {
			view=inflater.inflate(R.layout.fragment_classification, null);
			xlistview = (XListView) view.findViewById(R.id.xListView);
			NoNetWork = (ImageView) view.findViewById(R.id.image_no_wifi);
			mRootView = new WeakReference<View>(view);
//	        	httpBanner();
		} else {
			ViewGroup parent = (ViewGroup) mRootView.get().getParent();
			if (parent != null) {
				parent.removeView(mRootView.get());
			}
		}
//		View = inflater.inflate(R.layout.fragment_classification, container, false);

		initView();
		return mRootView.get();
	}
	//	View v ;
	private void initView() {
		adapter = new FragmentAdapter01(getActivity());
		xlistview.setAdapter(adapter);
//		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(this);
		if(NetWorkUtils.getAPNType(getActivity())==0){
			xlistview.setVisibility(View.GONE);
			NoNetWork.setVisibility(View.VISIBLE);
		}else{
			xlistview.setVisibility(View.VISIBLE);
			NoNetWork.setVisibility(View.GONE);
			httpLaberGroupList();
		}
		NoNetWork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(getActivity())==0){
					xlistview.setVisibility(View.GONE);
					NoNetWork.setVisibility(View.VISIBLE);
					ToastUtil.showShort(getActivity(), "断网啦！");
				}else{
					xlistview.setVisibility(View.VISIBLE);
					NoNetWork.setVisibility(View.GONE);
					httpLaberGroupList();
				}

			}
		});
//		v = LayoutInflater.from(getActivity()).inflate(R.layout.banner_classification, null);
	}

//	/**
//	 * 首页广告图片获取
//	 */
//	private void httpBanner() {
//		HttpParams params = new HttpParams();
//		AppHttps.HttpPost(getActivity(),HttpInterface.ADLIST, params,
//				new AppHttpCallBack<AdlnfoListVo>(AdlnfoListVo.class, getActivity()) {
//					@Override
//					public void onSuccess(AdlnfoListVo result) {
//						if(result!=null&&result.adInfo.size()>0){
//							initViewPage(result.adInfo);
//						}
//
//					}
//
//					@Override
//					public void onFailures(int errorCode, String errorMsg) {
//						ToastUtil.showShort(getActivity(), errorMsg);
//					}
//				});
//
//	}

	/**
	 * 首页标签组获取
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
//		params.put("laberId", id);
//		params.put("bLaberId","");//所属病种ID
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(getActivity(),HttpInterface.ART_PROJECT_LIST, params,
				new AppHttpCallBack<artGroupListVo>(artGroupListVo.class, getActivity()) {
					@Override
					public void onSuccess(artGroupListVo result) {
						boolean hasMore = true;
						artGroupVos = result.artInfo;
						if (refresh) {
							adapter.changeData(artGroupVos);
						} else {
							if(UtilsSp.getIsYm(getActivity())){
								artGroupVo vo=new artGroupVo();
								vo.isbanner=true;
								artGroupVos.add(vo);
							}
							adapter.addItems(artGroupVos );
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});

	}
//
//	/**
//	 * 分类banner展示
//	 */
//	private void initViewPage(List<AdlnfoVo> adInfo) {
//	
//		
//		mAdapter = new ImageFragmentAdapter(getActivity().getSupportFragmentManager());
//		pager = (ViewPager) v.findViewById(R.id.pager);
//		pager.setAdapter(mAdapter);
//		mAdapter.setData(adInfo);
//		indicator = (CirclePageIndicator) v.findViewById(R.id.indicator);
//		indicator.setViewPager(pager,adInfo.size(),0);
//		xlistview.addHeaderView(v);
//		
//	}

//	@Override
//	public void onItemClick(AdapterView<?> arg0, android.view.View arg1, int arg2, long arg3) {
//		ToastUtil.showShort(getActivity(), arg2 + "");
//		List<laberInfoVo> laberInfoVos=adapter.getItems();
//		Intent intent = new Intent(getActivity(), ClassificationDetailActivity.class);
//		intent.putExtra("ID", laberInfoVos.get(arg2 - 2).laberId);
//		intent.putExtra("NAME", laberInfoVos.get(arg2 - 2).laberName);
//		intent.putExtra("IMG", laberInfoVos.get(arg2 - 2).laberImg);
//		getActivity().startActivity(intent);
//	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}
}